# Copyright (c) 2025 Li Auto Inc. and its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.6)

set(VBS_TRANSPORT "vbsTransport")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

message("CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
else()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
endif()

if (ANDROID_NDK_BUILD)
    set (ANDROID_PROTOBUF_DIR "${PROJECT_SOURCE_DIR}/install/android-arm64")
    add_compile_definitions(ANDROID)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -fPIC")
else()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -fPIC")
endif()

if (WIN32)
    set(VBSTRANSPORT_SOURCES
        ${CMAKE_CURRENT_SOURCE_DIR}/TransportFactory.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/ReceiverResource.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/ChannelResource.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/udp/UDPChannelResource.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/udp/UDPv4Transport.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/udp/UDPTransportInterface.cpp
        )
else ()
    set(VBSTRANSPORT_SOURCES
        ${CMAKE_CURRENT_SOURCE_DIR}/TransportFactory.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/ReceiverResource.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/ChannelResource.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/udp/UDPChannelResource.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/udp/UDPv4Transport.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/udp/UDPTransportInterface.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/uds/UDSDgramTransportInterface.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/uds/UDSDgramChannelResource.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/uds/UDSStreamTransportInterface.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/uds/UDSStreamChannelResource.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/uds/UDSStreamAcceptor.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/raw/RAWChannelResource.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/raw/RAWTransport.cpp
        )
endif()



set(TCP_TRANSPORT_COMPILE ON)
# TCP Transport
if(TCP_TRANSPORT_COMPILE)
    list(APPEND VBSTRANSPORT_SOURCES
        ${CMAKE_CURRENT_SOURCE_DIR}/tcp/TCPChannelResource.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/tcp/TCPChannelResourceBasic.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/tcp/TCPAcceptor.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/tcp/TCPAcceptorBasic.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/tcp/TCPv4Transport.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/tcp/TCPTransportInterface.cpp
        )
    # TLS Support
    if(TLS_FOUND)
    list(APPEND VBSTRANSPORT_SOURCES
        ${CMAKE_CURRENT_SOURCE_DIR}/tcp/TCPChannelResourceSecure.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/tcp/TCPAcceptorSecure.cpp
        )
    endif()
endif()

if (XDP_TRANSPORT_COMPILE AND NOT WIN32)
    # add_subdirectory(udp/lixdp)
    list(APPEND VBSTRANSPORT_SOURCES
        udp/LixdpSocketUDP.cpp
        )
    add_definitions(-DEDDS_XDP_TRANSPORT_COMPILE)
endif()

# SHM Transport
if(SHM_TRANSPORT_COMPILE)
    list(APPEND VBSTRANSPORT_SOURCES
        ${CMAKE_CURRENT_SOURCE_DIR}/shared_mem/test_SharedMemTransport.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/shared_mem/SharedMemTransport.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/shared_mem/SharedMemTransportDescriptor.cpp
        )
endif()

if(IPC_TRANSPORT_COMPILE)
    list(APPEND VBSTRANSPORT_SOURCES
        ${CMAKE_CURRENT_SOURCE_DIR}/ipc/IPCChannelResource.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/ipc/IPCTransport.cpp
        )

    set(HBIPC 1)
    add_definitions(-DEDDS_IPC_TRANSPORT)
    list(APPEND UNIFIED_DEFINES "-DEDDS_IPC_TRANSPORT")
else()
    set(HBIPC 0)
endif()

set(VBS_TRANSPORT_INCLUDE_DIR
    ${PROJECT_SOURCE_DIR}/include/transport
    CACHE
    PATH "Path to vbstransport")

if (CICD_PARAMS)
    message(--------------CICD_PARAMS-------------)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
endif()

# 将源文件添加到统一库的源文件列表中
list(APPEND UNIFIED_SOURCES ${VBSTRANSPORT_SOURCES})

# 将包含目录添加到统一库的包含目录列表中
list(APPEND UNIFIED_INCLUDE_DIRS
    ${PROJECT_SOURCE_DIR}/include/
    ${PROJECT_SOURCE_DIR}/src/
    ${PROJECT_SOURCE_DIR}/src/cpp/transport/raw/
    ${PROJECT_BINARY_DIR}/include
    ${Asio_INCLUDE_DIR}
)

if(TCP_TRANSPORT_COMPILE)
    list(APPEND UNIFIED_INCLUDE_DIRS
        ${ECDR_INCLUDE_PATH}
        ${CMAKE_CURRENT_SOURCE_DIR}
        ${CMAKE_CURRENT_SOURCE_DIR}/tcp
    )
endif()


# 将变量传递回父作用域
set(UNIFIED_SOURCES ${UNIFIED_SOURCES} CACHE INTERNAL "Unified sources for all libraries")
set(UNIFIED_INCLUDE_DIRS ${UNIFIED_INCLUDE_DIRS} CACHE INTERNAL "Unified include directories for all libraries")
set(UNIFIED_LINK_LIBS ${UNIFIED_LINK_LIBS} CACHE INTERNAL "Unified link libraries for all libraries")
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} CACHE INTERNAL "Unified cxx flags for all libraries")
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} CACHE INTERNAL "Unified c flags for all libraries")
set(UNIFIED_DEFINES ${UNIFIED_DEFINES} CACHE INTERNAL "Unified defines for all libraries")
